package helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Oleh
 */
public class SimpleJSONObject {

    private Map map;

    public SimpleJSONObject() {
        map = new HashMap();
    }

    /**
     * Add property
     *
     * @param propertyName
     * @param propertyValue
     */
    public void addProperty(String propertyName, String propertyValue) {
        map.put(propertyName, propertyValue);
    }

    /**
     * Return JSON text
     *
     * @return
     */
    public String toString() {
        String result = "{\n";
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            Object value = map.get(key);            
            key = key.replaceAll("\"", "&quote;");
            value = value.toString().replaceAll("\"", "&quote;");
            result += "\t\"" + key + "\": \"" + value + "\",\n";
        }
        if (map.size() > 0) {
            result = result.substring(0, result.length() - 2);
        }
        result += "\n}";
        return result;
    }
}
